<?php
namespace app\admin\controller;
use think\Controller;
use think\View;
use think\Db;
use think\Url;
use think\Config;
use think\Request;
use app\admin\models\Goods;
use app\admin\models\Attachment;
use app\admin\models\Brand;
use app\admin\models\Category;
class Product extends Controller
{	//多图
	public function goods_thumd(){
        $file=request()->file("file");
        if($file){
            $info=$file->validate(['ext'=>'jpg,png,gif,jpeg'])
            ->rule('md5')
            ->move(ROOT_PATH.'public'.DS.'data/goods');
            if($info){
                $urls=$info->getSaveName();
            }
            $url='/data/goods/'.$urls;
            $data=[
                'form_uniqid'=>input('post.form_uniqid'),
                'dateline'=>time(),
                'url'=>$url,
            ];
            $att=Attachment::create($data);
            if($att){
                echo json_encode(['success'=>1]);
            }else{
                echo json_encode(['success'=>0]);
            }
       }
    }
	 public function add()
    {	
    	$goods = new Goods;
		if ($_POST) {	
			$goods->goods_name = $_POST['goods_name'];
			$goods->description = $_POST['description'];
			$goods->form_uniqid = $_POST['form_uniqid'];
			$goods->price = $_POST['price'];
			$goods->content = $_POST['content'];
			$goods->stock = $_POST['stock'];			
			$goods->keywords = $_POST['keywords'];
			$goods->origin = $_POST['origin'];
			$goods->brand = $_POST['brand'];
			$goods->goods_sn = $_POST['goods_sn'];
			$goods->category_id = $_POST['category'];
			$goods->size = $_POST['size'];	
			$goods->color = $_POST['color'];
			$goods->weight = $_POST['weight'];
			$goods->status= $_POST['status'];
			$goods->inputtime = $_POST['inputtime'];
			$goods->updatetime = $_POST['updatetime'];		
				$file = request()->file('thumb');		
		if($file){
			$path = '/data/goods/'.date('Ymd',time());
			$info=$file->move(ROOT_PATH.'public'.DS.'data/goods/');
			if($info){
				$goods->thumb=$path.'/'.$info->getFilename();
			}else{
				echo $file->getError();
			}
		}
			
			$g=$goods->save();
			if($g){
				$this->success('添加成功',url('admin/product/lists'));
			}else{
				$this->error('添加失败',url('admin/product/add'));
			}
		}else{
			$form_uniqid = uniqid();
			$cs = Category::all()->toArray();				
			 return $this->fetch('product_add',['form_uniqid'=>$form_uniqid,'cs'=>$cs]);
		}
		
	
	
	}


	 public function edit()
    {	$pid=input('param.goods_id');
    	
		if ($_POST) {
			$goods = Goods::get($pid);	
			$goods->goods_name = $_POST['goods_name'];
			$goods->description = $_POST['description'];
			$goods->form_uniqid = $_POST['form_uniqid'];
			$goods->price = $_POST['price'];
			$goods->content = $_POST['content'];
			$goods->stock = $_POST['stock'];			
			$goods->keywords = $_POST['keywords'];
			$goods->origin = $_POST['origin'];
			$goods->brand = $_POST['brand'];
			$goods->goods_sn = $_POST['goods_sn'];
			$goods->category_id = $_POST['category'];
			$goods->size = $_POST['size'];	
			$goods->color = $_POST['color'];
			$goods->weight = $_POST['weight'];
			$goods->status= $_POST['status'];
			$goods->inputtime = $_POST['inputtime'];
			$goods->updatetime = $_POST['updatetime'];		
				$file = request()->file('thumb');		
		if($file){
			$path = '/data/goods/'.date('Ymd',time());
			$info=$file->move(ROOT_PATH.'public'.DS.'data/goods/');
			if($info){
				$goods->thumb=$path.'/'.$info->getFilename();
			}else{
				echo $file->getError();
			}
		}
			
			$g=$goods->save();
			if($g){
				$this->success('修改成功',url('admin/product/lists'));
			}else{
				$this->error('修改失败',url('admin/product/add'));
			}
		}else{
			$gd=Goods::get($pid)->toArray();
			$ct=Category::get(['category_id'=>$gd['category_id']])->toArray();
				//print_r($ct);exit;		
			 return $this->fetch('product_add',['gd'=>$gd,'ct'=>$ct]);
		}
		
	
	
	}

	public function pdel()
    {	
    	$gid=input('param.goods_id');
    	$g=Goods::destroy($gid);
    	if($g){
				$this->success('删除成功',url('admin/product/lists'));
			}else{
				$this->error('删除失败',url('admin/product/lists'));
			}
	 return $this->fetch('product_list');
	
	}
    public function brand()
    {	$brd=new Brand;
    	$brand=$brd->order('brand_id', 'asc')->select()->toArray();
		$this->assign('brand',$brand);
	 return $this->fetch('product_brand');
	
	}
	public function brand_add()
    {	if($_POST){
       		$brand = new Brand;
			$data = array(
				'bname' => $_POST['bname'],	
				'sorting' => $_POST['sorting'],	
				'describe' => $_POST['describe'],						
				'dateline' => time()
			);	
			$file = request()->file('thumb');		
		if($file){
			$path = '/data/product/'.date('Ymd',time());
			$info=$file->move(ROOT_PATH.'public'.DS.'data/product/');
			if($info){
				$data['thumb']=$path.'/'.$info->getFilename();
			}else{
				echo $file->getError();
			}
		}
			$rt=$brand->insert($data);
			if($rt){
				$this->success('添加成功',url('admin/product/brand'));
			}else{
				$this->error('添加失败',url('admin/product/brand_add'));
			}

		}else{
			 return $this->fetch('product_brand_add');
		}
		
	
	
	}
	
	public function brand_edit()
    {
    	$brand = new Brand;
    	$bid=input('param.brand_id');
    	if($_POST){
       		$brand = new Brand;
			$data = array(
				'bname' => $_POST['bname'],	
				'sorting' => $_POST['sorting'],	
				'describe' => $_POST['describe'],						
				'dateline' => time()
			);	
			$file = request()->file('thumb');		
		if($file){
			$path = '/data/product/'.date('Ymd',time());
			$info=$file->move(ROOT_PATH.'public'.DS.'data/product/');
			if($info){
				$data['thumb']=$path.'/'.$info->getFilename();
			}else{
				echo $file->getError();
			}
		}
			$rt=$brand->where('brand_id',$bid)->update($data);
			if($rt){
				$this->success('修改成功',url('admin/product/brand'));
			}else{
				$this->error('修改失败',url('admin/product/brand_add'));
			}

		}else{
			$br = $brand->where('brand_id',$bid)->find()->toarray();
			//print_r($br);exit;
			 return $this->fetch('product_brand_add',['br'=>$br]);
		}
		
	
	
	}

	public function brand_del()
    {	
    	$bid=input('param.brand_id');
    	$b=Brand::destroy($bid);
    	if($b){
				$this->success('删除成功',url('admin/product/brand'));
			}else{
				$this->error('删除失败',url('admin/product/brand'));
			}
	 return $this->fetch('product_brand');
	
	}


	public function lists()
    {	$god=Goods::all()->toArray();
    	//print_r($god);exit;
	 return $this->fetch('product_list',['god'=>$god]);
	
	}

	//分类

	 public function cat_add()
    {
    	if($_POST){
			$data=[
				'category_pid'=>$_POST['pid'],				
				'category_name'=>$_POST['category-name'],
				'dateline'=>time()
				];	
			
			if($_POST['pid'] == 0){
				$data['category_grade'] = 1;
			}else{
				$prs = Db::table('lsq_product_category')->find($_POST['pid']);
				$data['category_grade'] = $prs['category_grade'] + 1;
			}
			
			$ret = Db::table('lsq_product_category')->insert($data);
			if($ret){
				$this->success('添加成功',url('admin/product/cat_lists'));
			}else{
				$this->error('添加失败',url('admin/product/cat_add'));
			}
		}
		return $this->fetch('product_category_add');	
	}

	 public function cat_lists()
    {
		
			$data = get_cate_data();
			$this->assign('data',$data);

	 		return $this->fetch('product_category');
	
	}
}
?>